import { ItemDefine } from "../../../kds-base-define/src/ItemDefine";
import { DB } from "../db";


let redis = DB.redis
let configTableName = "t_item_config"
let key = "item"
let cacheConfig:ItemDefine.ItemConfigData[] 
let initPromise:Promise<void>
export namespace ItemHelper {

	export async function getItemConfig(itemID:number) {
		if(initPromise) {
			await initPromise
		}
		if(cacheConfig == null) {
			return null 
		}
		return cacheConfig.find(v=>v.itemID == itemID)
	}

	export async function getAllItemConfigs() {
		if(initPromise) {
			await initPromise
		}
		return cacheConfig.slice(0)
	}

	export async function setConfig(items:ItemDefine.ItemConfigData[]) {
		if(initPromise) {
			await initPromise
		}
		cacheConfig = items 
		await redis.hset(configTableName,key,items,true)
	}

	export async function init() {
		cacheConfig = await redis.hget(configTableName,key,true)
		if(cacheConfig == null) {
			cacheConfig = []
		}
		initPromise = null 
	}
}

initPromise = ItemHelper.init()

